package com.wswy.wzcx.ui.car.result

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.DecelerateInterpolator
import android.widget.TextView
import com.airbnb.lottie.LottieAnimationView
import com.blankj.utilcode.util.SizeUtils
import com.wswy.wzcx.R
import timber.log.Timber

object ResultTips {

    private var isShowing = false

    fun reset(){
        isShowing = false
    }

    fun show(rootView:ViewGroup,success:Boolean,tips:CharSequence?){

        if (isShowing){
            return
        }

        Timber.e("---show----")

        isShowing = true

        LayoutInflater.from(rootView.context).inflate(R.layout.layout_query_result_tips,rootView,false)?.run {

            rootView.addView(this)

            findViewById<TextView?>(R.id.tv_content)?.text = tips

            if (success){
                findViewById<LottieAnimationView>(R.id.img_anim_status).setAnimation(R.raw.query_tips_sucessful)
            }else{
                findViewById<LottieAnimationView>(R.id.img_anim_status).setAnimation(R.raw.query_tips_error)
            }

            showAnim(this)

        }


    }

    private fun showAnim(view: View){
        ObjectAnimator.ofPropertyValuesHolder(view,
                PropertyValuesHolder.ofFloat(View.ALPHA,0f,1f),
                PropertyValuesHolder.ofFloat(View.TRANSLATION_Y,0f,SizeUtils.dp2px(60f).toFloat())

                ).apply {

            addListener(object : AnimatorListenerAdapter() {

                override fun onAnimationEnd(animation: Animator?) {
                    super.onAnimationEnd(animation)

                    view.postDelayed({

                        endAnim(view)

                    },1500)

                }
            })


            interpolator = DecelerateInterpolator()
            duration = 300

        }.start()
    }

    private fun endAnim(view: View){
        ObjectAnimator.ofPropertyValuesHolder(view,
                PropertyValuesHolder.ofFloat(View.ALPHA,1f,0f),
                PropertyValuesHolder.ofFloat(View.TRANSLATION_Y,view.translationY,0f)

        ).apply {

            addListener(object : AnimatorListenerAdapter() {

                override fun onAnimationEnd(animation: Animator?) {
                    super.onAnimationEnd(animation)

                    (view.parent as? ViewGroup)?.removeView(view)
                    isShowing = false
                }
            })

            interpolator = AccelerateDecelerateInterpolator()
            duration = 300

        }.start()
    }

}